//
//  BGHomeCell.swift
//  WeiboSwift
//
//  Created by 战场原黑仪 on 16/7/24.
//  Copyright © 2016年 战场原黑仪. All rights reserved.
//

import UIKit
import SnapKit

class BGHomeCell: UITableViewCell {
    
    var cellModel: BGHomeCellModel?{
        didSet{
            //给原创微博赋值
            originalView.cellModel = cellModel
            //给工具栏赋值
            toolBar.cellModel = cellModel
            //计算转发布局
            let retweet = cellModel?.statusModel?.retweeted_status
            //重新布局
            if retweet == nil {
                //重新为toolBar设置约束
                toolBar.snp_remakeConstraints { (make) in
                    make.top.equalTo(originalView.snp_bottom)
                    make.leading.trailing.equalTo(contentView)
                    make.height.equalTo(30)
                }
                retweetView.hidden = true
            }else {
                //为其赋值
                retweetView.cellModel = cellModel
                //重新为toolBar设置约束
                toolBar.snp_remakeConstraints { (make) in
                    make.top.equalTo(retweetView.snp_bottom)
                    make.leading.trailing.equalTo(contentView)
                    make.height.equalTo(30)
                }
                retweetView.hidden = false
            }
        }
    }
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.setupUI()
        self.setupLayout()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置界面
    private func setupUI() {
        self.backgroundColor = Main_Color
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(toolBar)
    }
    
    private func setupLayout() {
        originalView.snp_makeConstraints { (make) in
            make.top.leading.trailing.equalTo(contentView)
        }
        
        retweetView.snp_makeConstraints { (make) in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.equalTo(contentView).offset(ViewMargin_S)
            make.trailing.equalTo(contentView).offset(-ViewMargin_S)
        }
        
        toolBar.snp_makeConstraints { (make) in
            make.top.equalTo(retweetView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(30)
        }
        
        contentView.snp_makeConstraints { (make) in
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(toolBar).offset(ViewMargin_SS)
        }
    }
    
    //MARK: - 懒加载
    //原创微博界面
    private lazy var originalView: BGStatusOriginalView = BGStatusOriginalView()
    //转发微博界面
    private lazy var retweetView: BGStatusRetweetView = BGStatusRetweetView()
    //工具栏界面
    private lazy var toolBar: BGStatusToolBarView = BGStatusToolBarView()
    
    
}
